<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-box">
        <div class="login-header">
          <router-link to="/" class="logo">
            <img src="@/assets/headphone-logo.png" alt="Logo" />
          </router-link>
          <h2>欢迎登录</h2>
          <p>专注音频体验，聆听美好生活</p>
        </div>

        <form @submit.prevent="handleSubmit" class="login-form">
          <div class="form-group">
            <label>
              <i class="fas fa-user"></i>
              <input
                type="text"
                v-model="form.username"
                placeholder="请输入用户名/手机号/邮箱"
                required
              />
            </label>
          </div>

          <div class="form-group">
            <label>
              <i class="fas fa-lock"></i>
              <input
                :type="showPassword ? 'text' : 'password'"
                v-model="form.password"
                placeholder="请输入密码"
                required
              />
              <button
                type="button"
                class="toggle-password"
                @click="showPassword = !showPassword"
              >
                <i
                  :class="['fas', showPassword ? 'fa-eye-slash' : 'fa-eye']"
                ></i>
              </button>
            </label>
          </div>

          <div class="form-options">
            <label class="remember-me">
              <input type="checkbox" v-model="form.remember" />
              <span>记住我</span>
            </label>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>

          <button type="submit" class="submit-btn" :disabled="loading">
            {{ loading ? "登录中..." : "登录" }}
          </button>

          <div class="register-link">
            还没有账号？<router-link to="/register">立即注册</router-link>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    return {
      loading: false,
      showPassword: false,
      form: {
        username: "",
        password: "",
        remember: false,
      },
    };
  },
  methods: {
    async handleSubmit() {
      if (!this.form.username || !this.form.password) {
        alert("请填写用户名和密码");
        return;
      }

      try {
        this.loading = true;
        await this.$store.dispatch("login", {
          username: this.form.username,
          password: this.form.password,
        });
        alert("登录成功！");
        const redirect = this.$route.query.redirect || "/";
        this.$router.push(redirect);
      } catch (error) {
        alert(error.message);
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #fff6f6 0%, #ffe9e9 100%);
  padding: 40px 20px;
}

.login-container {
  width: 100%;
  max-width: 460px;
}

.login-box {
  background: white;
  border-radius: 10px;
  padding: 40px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  display: inline-block;
  margin-bottom: 20px;
}

.logo img {
  max-width: 150px;
  height: auto;
}

.login-header h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

.form-group label {
  display: block;
  width: 100%;
}

.form-group i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  z-index: 1;
  pointer-events: none;
}

.form-group input {
  width: 100%;
  height: 44px;
  padding: 0 45px;
  border: 1px solid #ddd;
  border-radius: 22px;
  font-size: 14px;
  transition: all 0.3s;
}

.form-group input:focus {
  border-color: #ff6b6b;
  box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.1);
  outline: none;
}

.toggle-password {
  position: absolute;
  right: 15px;
  top: 0;
  height: 44px;
  background: none;
  border: none;
  color: #999;
  cursor: pointer;
  padding: 0 15px;
  display: flex;
  align-items: center;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #666;
  font-size: 14px;
  cursor: pointer;
}

.remember-me input {
  width: 16px;
  height: 16px;
  accent-color: #ff6b6b;
}

.forgot-password {
  color: #ff6b6b;
  font-size: 14px;
  text-decoration: none;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background: #ff6b6b;
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.submit-btn:hover:not(:disabled) {
  background: #ff5252;
}

.submit-btn:disabled {
  background: #ffb3b3;
  cursor: not-allowed;
}

.register-link {
  text-align: center;
  color: #666;
  font-size: 14px;
}

.register-link a {
  color: #ff6b6b;
  text-decoration: none;
  margin-left: 5px;
}

@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
  }
}
</style> 